package com.sl.water.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sl.common.exception.SchemeNotFoundException;
import com.sl.water.dao.PlanDynamicDao;
import com.sl.water.dto.PlanDynamicDTO;
import com.sl.water.entity.PlanDynamic;
import com.sl.water.entity.RehearsalScheme;
import com.sl.water.service.PlanControlFhzService;
import com.sl.water.service.PlanDynamicService;
import com.sl.water.service.PlanTransferService;
import com.sl.water.service.RehearsalSchemeService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @description:预案表 服务接口实现
 * @author: duan
 * @time: 2025-06-26 14:37
 */
@Service
@RequiredArgsConstructor
public class PlanDynamicServiceImpl extends ServiceImpl<PlanDynamicDao, PlanDynamic> implements PlanDynamicService {

    private final PlanTransferService planTransferService;
    private final PlanControlFhzService planControlFhzService;
    private final RehearsalSchemeService rehearsalSchemeService;

    @Override
    @Transactional
    public PlanDynamic createByYY(Long rehearsalId) {
        RehearsalScheme rehearsalScheme = rehearsalSchemeService.getById(rehearsalId);
        if (ObjectUtil.isNull(rehearsalScheme)) {
            throw new SchemeNotFoundException("预演方案不存在");
        }
        PlanDynamic planDynamic = PlanDynamic.builder()
                .name(rehearsalScheme.getName() + "-预案")
                .state(0)
                .rehearsalId(rehearsalId).build();
        this.save(planDynamic);
        planControlFhzService.saveByRehearsalScheme(planDynamic.getId(), rehearsalScheme);
        planTransferService.saveByRehearsalScheme(planDynamic.getId(), rehearsalScheme);
        return planDynamic;
    }

    @Override
    public void removeByRehearsalId(Long rehearsalId) {
        LambdaQueryWrapper<PlanDynamic> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlanDynamic::getRehearsalId, rehearsalId);
        this.remove(lambdaQueryWrapper);
    }

    @Override
    public Page<PlanDynamic> pageList(int page, int size, String name) {
        LambdaQueryWrapper<PlanDynamic> queryWrapper = new LambdaQueryWrapper<>();
        Page<PlanDynamic> dynamicPage = new Page<>(page, size);
        if (ObjectUtils.isNotEmpty(name)) {
            queryWrapper.like(PlanDynamic::getName, name);
        }
        queryWrapper.orderByDesc(PlanDynamic::getCreatedTime);
        return this.page(dynamicPage, queryWrapper);
    }

    @Override
    public PlanDynamicDTO getPlanDynamicDTOById(Long id) {
        PlanDynamic planDynamic = this.getById(id);
        if(ObjectUtil.isNull(planDynamic)){
            throw new RuntimeException("预案信息不存在!");
        }
        long rehearsalId = planDynamic.getRehearsalId();
        RehearsalScheme rehearsalScheme = rehearsalSchemeService.getById(rehearsalId);
        return PlanDynamicDTO.builder()
                .id(id)
                .name(planDynamic.getName())
                .rehearsalId(rehearsalId)
                .rehearsalName(rehearsalScheme.getName())
                .attach(planDynamic.getAttach())
                .controlFhzList(planControlFhzService.queryByPlanID(id))
                .transferList(planTransferService.queryByPlanId(id))
                .build();
    }

    @Override
    public boolean updateState(long id, Integer state) {
        PlanDynamic planDynamic = new PlanDynamic();
        planDynamic.setState(state);
        UpdateWrapper<PlanDynamic> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        return this.update(planDynamic, updateWrapper);
    }

    @Override
    @Transactional
    public void removeData(long id) {
        this.removeById(id);
        planTransferService.removeByPlanId(id);
        planControlFhzService.removeByPlanId(id);
    }


}
